//
//  SimpleMenuListViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 17/1/2.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "SimpleMenuListViewController.h"
#import "SimpleCollectionViewCell.h"

#import "AddWorkSummaryViewController.h"
#import "MyWorkSummaryViewController.h"

#import "AddAttendanceViewController.h"
#import "MyAttendanceViewController.h"

NSString *const SimpleMenuListViewControllerTitleParamKey = @"SimpleMenuListViewControllerTitleParamKey";
NSString *const SimpleMenuListViewControllerMenuCodeParamKey = @"SimpleMenuListViewControllerMenuCodeParamKey";

@interface SimpleMenuListViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,SimpleCollectionViewCellDelegate>
{
    NSMutableArray *_qdData;
    RSWDataOperation *_manager;
    
    NSInteger _itemCount;
    
    XUIGroupTableView *_gtable;
}
@end

@implementation SimpleMenuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    NSString *title = self.param[SimpleMenuListViewControllerTitleParamKey];
    [self setBackNavigateItemTitle:title];
    
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    
    [self initVariable];
    
    NSString *_code = self.param[SimpleMenuListViewControllerMenuCodeParamKey];
    [self addKeyboardObserver];
    
    [self initUI];
    
    [_manager getSubMenuListByParentCode:_code Completion:^(NSInteger nResult, NSArray *output, NSError *error) {
        [self requestComplete:nil result:nResult output:output error:error];
    }];
}

- (void)initVariable
{
    _qdData = [NSMutableArray new];
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];

    _itemCount = 4;
}

- (void)initUI
{
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _gtable.backgroundColor = [[UITableView appearance] backgroundColor];
    _gtable.delegate = self;
    _gtable.dataSource = self;
    
    [self.view addSubview:_gtable];
}

-(void)requestComplete:(XMSDataOperationManager *)dom result:(NSInteger)nResult output:(id)output error:(NSError *)error
{
    [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
     {
         switch(requestResult)
         {
             case YES:
             {
                 _qdData = output;
                 
                 [_gtable reloadData];
                 
                 break;
             }
             default:
             {
                 break;
             }
         }
     }];
}

#pragma mark --
#pragma mark -- XUIGroupTableViewDelegate XUIGroupTableViewDataSource
-(CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    return 0;
}

- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 1;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    return 1;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row ==0)
    {
        return [SimpleCollectionViewCell caleHeight:_qdData.count itemCount:_itemCount itemHeight:1 hiddenTitle:NO];
    }
    
    return 44;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SimpleCollectionViewCell *cell = [[SimpleCollectionViewCell alloc] initWithFrame:CGRectZero];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.itemCount = _itemCount;
    cell.uiEdgeInsets = UIEdgeInsetsMake(1, 1, 1, 1);
    cell.title = @"";
    cell.hiddenTitle = YES;
    cell.delegate = self;
    cell.qdData = [_qdData mutableCopy];
    
    return cell;
}

#pragma --
#pragma -- SimpleCollectionViewCellDelegate
-(void)simpleCollectionViewCell:(SimpleCollectionViewCell*)cell clickedButton:(id)sender info:(MenuInfo*)info;
{
    if([info.code isEqualToString:@"新增工作纪要"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[AddWorkSummaryViewControllerTitleParamKey] =  info.name;
        
        [XUINavigationController pushViewControllerClass:[AddWorkSummaryViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"我的工作纪要"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[MyWorkSummaryViewControllerTitleParamKey] =  info.name;
        p.param[MyWorkSummaryViewControllerSourceTypeParamKey] = @(MyWorkSummarySource);
        
        [XUINavigationController pushViewControllerClass:[MyWorkSummaryViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"领导点评"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[MyWorkSummaryViewControllerTitleParamKey] =  info.name;
        p.param[MyWorkSummaryViewControllerSourceTypeParamKey] = @(LeaderSummarySource);
        
        [XUINavigationController pushViewControllerClass:[MyWorkSummaryViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"我的考勤"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[MyAttendanceViewControllerTitleParamKey] =  info.name;
        
        [XUINavigationController pushViewControllerClass:[MyAttendanceViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"考勤上报"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[AddAttendanceViewControllerTitleParamKey] =  info.name;
        
        [XUINavigationController pushViewControllerClass:[AddAttendanceViewController class] withSwitchParam:p];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
